﻿using System;
using System.Reflection;

namespace Trentacular.SharePoint.Reflection
{
    public abstract class ReflectingObjectWrapper
    {
        protected abstract object Target { get; }

        private Type _targetType;

        /// <summary>
        /// Gets the type of the search results hidden object.
        /// </summary>
        /// <value>The type of the search results hidden object.</value>
        protected Type TargetType
        {
            get
            {
                if (_targetType == null)
                {
                    if (this.Target == null)
                        throw new ArgumentException("The target object is null");

                    _targetType = this.Target.GetType();
                }
                return _targetType;
            }
        }

        protected T GetPublicPropertyValue<T>(string propertyName)
        {
            return (T)this.TargetType
                .GetProperty(propertyName)
                .GetValue(this.Target, null);
        }

        protected object InvokeNonPublicInstanceMethod(string methodName, params object[] args)
        {
            return this.TargetType
                .GetMethod(
                    methodName,
                    BindingFlags.NonPublic | BindingFlags.Instance
                    )
                .Invoke(this.Target, args);
        }

//        protected object InvokePublicStaticMethod(string methodName, params 
    }
}